/******************************************************************************
 * Copyright (c) 2008 Savino Sguera. All rights reserved. 
 *
 * This program and the accompanying materials are made available under the 
 * terms of the Eclipse Public License v1.0, which accompanies this 
 * distribution and is available at http://www.eclipse.org/legal/epl-v10.html.
 *
 * Contributors:
 * 	Savino Sguera < s.sguera@ieee.org >
 ******************************************************************************
 */

package im.agents.slachecker;

import java.util.HashMap;
import java.util.Map;

import im.agents.IMAgent;
import im.agents.migration.ContextMonitoring;
import im.domain.MonitorData;
import im.domain.SLA;
import jade.core.AID;
import jade.core.Agent;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.domain.FIPANames.InteractionProtocol;
public class SLAChecker extends IMAgent {

	public static int ROLE_REQUESTER=0;
	public static int ROLE_PROVIDER=1;
	private Map<String, AID> subscribedSLAs = new HashMap<String,AID>();
	
	
	@Override
	public ServiceDescription getServiceDescription() {
		ServiceDescription service = new ServiceDescription();
		service.setName("SLAChecker");
		service.setType("SLAChecker");
		return service;
	}
	
	/* (non-Javadoc)
	 * @see im.agents.IMAgent#setup()
	 * Ricorda di ripristinare la migrazione!!!!
	 */
	public void setup(){
		super.setup();
		this.addBehaviour(new SLAProposeResponder(this, 
				getProposeResponderTemplate()));
		this.initializeTHRESHOLD(10, 500, 500);//mettere valori sensati


		addBehaviour(new ContextMonitoring(this,20000,this.getTHRESHOLD()));
	}
	
	public void takeDown(){
		super.takeDown();
	}
	
	private MessageTemplate getProposeResponderTemplate(){
		MessageTemplate t1=MessageTemplate.MatchPerformative(ACLMessage.PROPOSE);
		MessageTemplate t2=MessageTemplate.MatchProtocol(InteractionProtocol.FIPA_PROPOSE);
		
		return MessageTemplate.and(t1, t2);
	}
	
	
	public boolean checkRespectedSLA(SLA sla, MonitorData monitored, int role){
		if(sla==null||monitored==null)return false;

		if(role==ROLE_PROVIDER){
			System.out.println("\t\t\tPROVIDER");
			//tasso arrivi
			Float tassoArrivi=0.0f;
			Integer somma=0;
			if(monitored.getTempiInterarrivo()!=null){
				for(Integer i : monitored.getTempiInterarrivo()){
					somma+=i;
				}
				tassoArrivi=((float)monitored.getTempiInterarrivo().size())/((float)somma);
				System.out.println("\t\t\ttassoArrivi"+tassoArrivi+" - "+sla.getTassoArriviMax());	
				if(sla.getTassoArriviMax()!=null && tassoArrivi>=sla.getTassoArriviMax())return false;
			}
		}
		
		else if(role==ROLE_REQUESTER){
			System.out.println("\t\t\tREQUESTER");
			//success rate
			if(monitored.getReqSoddisfatte()!=null && monitored.getReqRicevute()!=null){
				Float successRate = ((float)monitored.getReqSoddisfatte())/((float)monitored.getReqRicevute());
				if(sla.getSuccessRateMin()!=null
						&&successRate<sla.getSuccessRateMin())return false;
			}
	
			//tempo risposta
			if(monitored.getTempoRispLast()!=null 
					&&monitored.getTempoRispLast()>sla.getTempoRispMax())
				return false;
		}
		return true;
	}

	public Map<String, AID> getSubscribedSLAs() {
		return subscribedSLAs;
	}

	public void setSubscribedSLAs(Map<String, AID> subscribedSLAs) {
		this.subscribedSLAs = subscribedSLAs;
	}
}
